package com.itheima.service.db;

import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.domain.db.User;
import com.itheima.mapper.UserMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;

@DubboService //1.加入spring容器  //2.暴露服务方法
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Override
    public Long save(User user) {

        //对密码进行加密
        user.setPassword(SecureUtil.md5(user.getPassword()));

        //user.setCreated(new Date());
        //user.setUpdated(new Date());

        userMapper.insert(user);

        //返回新增记录的id
        return user.getId();
    }

    @Override
    public User findByPhone(String phone) {
        //1.创建用户的条件查询对象
        QueryWrapper<User> wraper = new QueryWrapper<>();
        wraper.eq("mobile",phone);

        //2.调用mapper方法进行条件查询
        return userMapper.selectOne(wraper);
    }
    // 通过用户id查询用户信息
    @Override
    public User findByUserId(Long userId) {
        //1.创建用户的条件查询对象
        QueryWrapper<User> wraper = new QueryWrapper<>();
        wraper.eq("id",userId);
        return userMapper.selectOne(wraper);
    }


    @Override
    public void update(User user) {
        userMapper.updateById(user);
    }
}
